package com.ctshk.rpc.order.season.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ctshk.common.enums.IsDeletedCode;
import com.ctshk.rpc.order.season.entity.SeasonOrderStock;
import com.ctshk.rpc.order.season.mapper.SeasonOrderMapper;
import com.ctshk.rpc.order.season.mapper.SeasonOrderStockMapper;
import com.ctshk.rpc.order.season.service.SeasonOrderStockService;
import com.ctshk.rpc.season.dto.SeasonProOfflineAgentCustomDTO;
import com.ctshk.rpc.season.service.ISeasonProductChannelOfflineAgentCustomService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.Date;

/**
 * <p>
 * 套票库存售卖记录表 服务实现类
 * </p>
 *
 * @author Eric
 * @since 2021-04-19
 */
@DubboService
public class SeasonOrderStockServiceImpl extends ServiceImpl<SeasonOrderStockMapper, SeasonOrderStock> implements SeasonOrderStockService {


    @DubboReference
    private ISeasonProductChannelOfflineAgentCustomService productChannelOfflineAgentCustomService;
    @Autowired
    private SeasonOrderStockMapper seasonOrderStockMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void operateStock(Long orderId, Long stockId, int category, int soldNum, int state, String checkType, Long userId) {
        SeasonOrderStock seasonOrderStock = new SeasonOrderStock();
        seasonOrderStock.setStockId(stockId);
        seasonOrderStock.setOrderId(orderId);
        seasonOrderStock.setSoldNumber(soldNum);
        seasonOrderStock.setState(state);
        seasonOrderStock.setChannel(category);
        seasonOrderStock.setCheckType(category);
        seasonOrderStock.setIsDeleted(IsDeletedCode.NO.getCode());
        LocalDateTime now = LocalDateTime.now();
        seasonOrderStock.setSalesDate(new Date());
        seasonOrderStock.setGmtCreate(now);
        seasonOrderStock.setGmtModified(now);
        seasonOrderStock.setCreateId(userId);
        seasonOrderStock.setModifiedId(userId);
        if (1 == category) {
            seasonOrderStock.setLine(1);
        } else if (2 == category) {
            seasonOrderStock.setLine(1);
            SeasonProOfflineAgentCustomDTO proOfflineAgentCustomDTO = productChannelOfflineAgentCustomService.queryAgent(stockId);
        } else if (3 == category) {
            seasonOrderStock.setLine(2);
        } else if (4 == category) {
            seasonOrderStock.setLine(2);
        }
    }

    @Override
    public void backToStock(Long orderId, Long productId, Long attrId1, Long attrId2, Integer ticketNum, Long userId) {

    }

    public SeasonOrderStock getSeasonOrderStock(String stockId) {
        QueryWrapper<SeasonOrderStock> qw = new QueryWrapper<>();
        qw.eq("stock_id", stockId);
        qw.eq("is_deleted", 0);//没有删除
        qw.last("limit 1");
        return seasonOrderStockMapper.selectOne(qw);

    }
}
